package com.burst.reptile.modular.api.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.burst.reptile.common.constant.Constants;
import com.burst.reptile.common.exception.CustomException;
import com.burst.reptile.common.exception.user.CaptchaException;
import com.burst.reptile.common.exception.user.CaptchaExpireException;
import com.burst.reptile.framework.redis.RedisCache;
import com.burst.reptile.framework.web.controller.BaseController;
import com.burst.reptile.framework.web.result.LayuiResult;
import com.burst.reptile.framework.web.result.Result;
import com.burst.reptile.modular.zp.entity.ZpFeedback;
import com.burst.reptile.modular.zp.service.IZpFeedbackService;

import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 意见反馈 前端控制器
 * </p>
 *
 * @author wujin
 * @since 2020-07-11
 */
@RestController
@RequestMapping("/zp/feedback")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ZpFeedbackController extends BaseController{

	private final IZpFeedbackService feedbackService;
	
	private final RedisCache redisCache;
	
	/**
	 * 添加意见反馈
	 * @param feedback
	 * @return
	 */
	@PostMapping("/add")
	public Result<Object> add(@Validated @RequestBody ZpFeedback feedback) {
		String uuid = feedback.getUuid();
		String code = feedback.getCode();
		String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
		String captcha = redisCache.getCacheObject(verifyKey);
		redisCache.deleteObject(verifyKey);
		if (captcha == null) {
			throw new CaptchaExpireException();
		}
		if (!code.equalsIgnoreCase(captcha)) {
			throw new CaptchaException();
		}
		return Result.success(feedbackService.add(feedback));
	}
	
	/**
	 * 意见反馈列表
	 * @param feedback
	 * @return
	 */
	@GetMapping("/list")
	@PreAuthorize("@ss.hasPermi('zp:feedback:list')")
	public LayuiResult<Object> list(ZpFeedback feedback) {
		Page<ZpFeedback> page = startPage();
		Page<ZpFeedback> pagelist = feedbackService.pageList(page, feedback);
		return LayuiResult.success(pagelist.getTotal(), pagelist.getRecords());
	}
	
	/**
	 * 处理结果
	 * @param feedback
	 * @return
	 */
	@PostMapping("/receipt")
	@PreAuthorize("@ss.hasPermi('zp:feedback:receipt')")
	public LayuiResult<Object> receipt(@Validated @RequestBody ZpFeedback feedback) {
		String fresult = feedback.getFresult();
		if(StrUtil.isEmpty(fresult)) {
			throw new CustomException("处理结果不能为空");
		}
		return LayuiResult.success(feedbackService.receipt(feedback));
	}
	
}
